<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>       
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<!DOCTYPE html>
<html style="overflow:hidden;">
<head>
<link rel="shortcut icon" href="${_resources}/logo.png"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">                                            
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE8" />  
<title>选择文档模板分类</title>
<%@ include file="/WEB-INF/jsp/common/inc.jsp" %>
<!--  树结构  -->
<script type="text/javascript" src="${_resources}js/jquery.ztree.core.js"></script>
<link rel="stylesheet" href="${_resources}css/zTreeStyle.css" type="text/css">
<script type="text/javascript" src="${_resources}js/jquery.ztree.excheck.js"></script>
<link href='${_resources}css/style.css' type='text/css' rel='stylesheet' />
<script type="text/javascript">
	var ztree;
	 var setting = {
            view: {
                dblClickExpand: false,
                showLine: true,
                selectedMulti: false,
            },
          /*   check: {
        		enable: true,
        		chkStyle: "checkbox",
        		chkboxType: {"Y": "", "N": ""}
        	}, */
            data: {
                simpleData: {
                    enable: true,
                    idKey: "id",
    				pIdKey: "parentId"
                }
            },
            callback: {
                onNodeCreated: this.onNodeCreated,
                beforeClick: zTreeBeforeClick,
                onClick: onClick
            }
        };
	var zNodes = eval('(${zNodes})');
	$(document).ready(function(){
		ztree = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
		nodes = ztree.getNodes(); 
		//ztree.selectNode(nodes[0].children[0]);
		var documentTypeId = "${documentTypeId}";
		if(documentTypeId != "" && typeof(documentTypeId) != "undefined"){
			var childrens = nodes[0].children;
			var num = 0;
			for (var i = 0; i < childrens.length; i++) {
				 if(childrens[i].id == documentTypeId){
					 num = i;
				 }
			}
			ztree.selectNode(nodes[0].children[num]);
		}else{
			ztree.selectNode(nodes[0].children[0])
		}
	});
	function addDiyDom(treeId, treeNode) {
		var liObj = $("#" + treeNode.tId);
		var aObj = $("#" + treeNode.tId + "_span");
		liObj.addClass("level_"+treeNode.level);
		aObj.addClass("le_"+treeNode.level);
	};
	
	//提交文档模板分类结果
	function formSubmit(){
		var documentId = "${documentId}";
		var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
		var nodes = treeObj.getSelectedNodes();
		var checkedId = "";
		if(nodes.length == 0){
			layer.msg("请选中一条数据进行修改");
			return false;
		}else{
			if(nodes.length > 1){
				for(var i = 0; i < nodes.length; i++){
					if(nodes[i].name == "软件设计类" || nodes[i].name == "软件测试类" || nodes[i].name == "软件管理类"){
						for(var j = i+1; j < nodes.length; j ++){
							if(nodes[j].name == "软件设计类" || nodes[j].name == "软件测试类" || nodes[j].name == "软件管理类"){
								layer.msg("软件设计类、软件测试类、软件管理类只可单选");
								return false;
							}
						}
					}
					
				}
				
				for(var i = 0; i < nodes.length; i ++){
					if(nodes[i].name != null && nodes[i].id != null){
						if(nodes[i].id == "root" ){
							layer.msg("请选择文档模板类型");
							return false;
						}
						checkedId += nodes[i].id+",";
					}
					
				}
				}else{
					checkedId += nodes[0].id+",";
				}
			}
		
		if(checkedId == "root," ){
			layer.msg("请选择文档模板类型");
			return false;
		}
		var options = {
				dataType:"json",
				data:{
					"checkedId" : checkedId,
					"documentId" : documentId
					
				},
				success:function(json){
						layer.msg(json.message,{shift:2,time:2000},function(){
							parent.closeWin();
							parent.refreshTable();
						});
				},
				error:function(json){
					layer.msg("系统错误,请联系管理员");
				}
			};
		if($("#form").validationEngine('validate')){
			$('#form').ajaxSubmit(options);
		}
	}
	//关闭弹出框
	function closeWin(){
		parent.closeWin();
	}
	//点击ztree节点时的事件
	function onClick(e,treeId, treeNode) {
		var zTree = $.fn.zTree.getZTreeObj("treeDemo");
		zTree.expandNode(treeNode);
	}
	
    function zTreeBeforeClick(treeId, treeNode, clickFlag) {
	    return !treeNode.isParent;//当是父节点 返回false 不让选取
};
</script>
    <style type="text/css">
        .main_left{
            position:fixed;
            top:42px;
            bottom:0;
            left:0;
            z-index:1000;
            width:450px;
            height:260px;
            border-right:0px;
        }
    .ztree li span.button.ico_open,.ztree li span.button.ico_docu,.ztree li span.button.ico_close{width:0px}
    .ztree li a{width:330px;overflow: hidden;/*内容超出后隐藏*/text-overflow: ellipsis;/* 超出内容显示为省略号*/white-space: nowrap;/*文本不进行换行*/}
    .main_left .ztree li a{line-height: 18px;}
    .main ul li a{
		padding-top:0px;
	}
	.ztree li a.level0{
		padding-top:0px;
	}
	.ztree li{
		line-height: 17px;
	}
    </style>
</head>
<body>
	<div class="submenu">
    <div class="submenu_b">
        <div class="fr submenu_b_b">
            <shiro:hasPermission name="standardTemplateSortPage:newlyAdded">
                <a class="current_1" id="newlyAdded"  onclick="formSubmit();">确定</a>
            </shiro:hasPermission>
            <shiro:hasPermission name="standardTemplateSortPage:close2">
                <a class="current_1" id="modify" onclick="closeWin();">取消</a>
            </shiro:hasPermission>
        </div>
    </div>
    </div>
<form action="${_baseUrl}/KmGroupDocumentTemplateController/updateTemplateSort" method="post" id="form" name="form">
	<input type="hidden" name="token" value="${token}" />
	<input type="hidden" id = "documentId" name="documentId" value="${documentId}"/>
    <div class="main">
        <div id="main-left" class="zTreeDemoBackground main_left">
            <div class="popup_tree">
                <ul id="treeDemo" class="ztree " style="overflow-x: hidden;overflow-y: auto;"></ul>
            </div>
        </div>
    </div>
</form>
</body>
</html>